package com.obas.integration.element.telnet.loader;

import java.util.ArrayList;
import java.util.List;

import org.springframework.util.StringUtils;

import com.ease.common.util.FileUtil;
import com.ease.common.util.OutputUtil;
import com.ease.common.vo.editor.AbstractObjectEditor;
import com.obas.integration.element.retriever.TelnetDeviceMacAddressLoader;
import com.obas.integration.element.telnet.editor.HWDefaultTelnetEditor;
import com.obas.model.business.DeviceMacAddress;

public class HWDefaultTelnetDeviceMacAddressLoader extends TelnetDeviceMacAddressLoader {

	public AbstractObjectEditor<DeviceMacAddress> getRawEditor() {
		return new HWDefaultTelnetEditor();
	}

	public List<DeviceMacAddress> editTextAsObject(String rawDeviceMacAddress) {
		AbstractObjectEditor<DeviceMacAddress> rawEditor = this.getRawEditor();

		String[] rows = StringUtils.delimitedListToStringArray(rawDeviceMacAddress, "\r\n");
		if (notEmpty(rows)) {
			List<DeviceMacAddress> deviceMacAddresses = new ArrayList<DeviceMacAddress>();
			boolean start = false;
			for (String row : rows) {
				if (!notEmpty(row)) {
					continue;
				}
				if ("  --------------------------------------------------------".equals(row)) {
					continue;
				}

				if ("  TYPE      MAC                 MAC TYPE   F/S     VLAN ID".equals(row)) {
					start = true;
					continue;
				}
				if (row.startsWith("  Total:")) {
					break;
				}
				if (true == start) {
					rawEditor.setAsText(row);
					DeviceMacAddress deviceMacAddress = rawEditor.getObject();
					if (null != deviceMacAddress) {
						deviceMacAddresses.add(deviceMacAddress);
					}
				}
			}
			return escapeEmpty(deviceMacAddresses);
		}
		return null;
	}

	public static void main(String[] args) {
		String rawDeviceMacAddress = FileUtil.readClassPathResourceToString(HWDefaultTelnetDeviceMacAddressLoader.class, "HW MA5616.txt");
		List<DeviceMacAddress> deviceMacAddresses = new HWDefaultTelnetDeviceMacAddressLoader().editTextAsObject(rawDeviceMacAddress);
		OutputUtil.p(deviceMacAddresses);
	}
}
